package com.lxyk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.lxyk.mapper.CustomerMapper;
import com.lxyk.pojo.Customer;
import com.lxyk.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CustomerServiceImpl
 * @Description TODO
 * @Author FJQ
 * @Date 2022/10/21 15:18
 * @Version 1.0
 **/
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper mapper;

    @Override
    public List<Customer> listCustomer() {
        return mapper.selectList(null);
    }

    @Override
    public int saveCustomer(Customer customer) {
        return mapper.insert(customer);
    }

    @Override
    public List<Customer> searchCustomer(String custName) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.like("cust_name",custName);
        return mapper.selectList(qw);
    }

    @Override
    public int deleteCustomerById(Integer cid) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq("cid",cid);
        return mapper.delete(qw);
    }

    @Override
    public Customer getCustomerById(Integer cid) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq("cid",cid);
        return mapper.selectOne(qw);
    }

    @Override
    public int updateCustomer(Customer customer) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq("cid",customer.getCid());
        return mapper.update(customer,qw);
    }


    /**
     * 根据客户名称和手机号搜索满足条件客户信息带分页
     * @param customer
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Customer> listCust(Customer customer, Integer pageNum, Integer pageSize) {
        List<Customer> customers = mapper.listCustomer(customer);
        return new PageInfo<>(customers);
    }
}
